package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.persistence.OrderDao;
import csu.web.mypetstore.persistence.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection connection;

    // 构造方法，注入数据库连接
    public OrderDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public List<Order> getOrdersByUsername(String username) {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM orders WHERE username = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, username);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setUsername(rs.getString("username"));
                order.setOrderDate(rs.getDate("order_date"));
                order.setShipAddress1(rs.getString("ship_address1"));
                order.setShipAddress2(rs.getString("ship_address2"));
                order.setShipCity(rs.getString("ship_city"));
                order.setShipState(rs.getString("ship_state"));
                order.setShipZip(rs.getString("ship_zip"));
                order.setShipCountry(rs.getString("ship_country"));
                order.setBillAddress1(rs.getString("bill_address1"));
                order.setBillAddress2(rs.getString("bill_address2"));
                order.setBillCity(rs.getString("bill_city"));
                order.setBillState(rs.getString("bill_state"));
                order.setBillZip(rs.getString("bill_zip"));
                order.setBillCountry(rs.getString("bill_country"));
                order.setCourier(rs.getString("courier"));
                order.setTotalPrice(rs.getBigDecimal("total_price"));
                order.setCreditCard(rs.getString("credit_card"));
                order.setExpiryDate(rs.getString("expiry_date"));
                order.setCardType(rs.getString("card_type"));
                order.setLocale(rs.getString("locale"));
                order.setStatus(rs.getString("status"));

                // 这里可能需要调用其他方法来填充订单的 `lineItems`（订单项）
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public Order getOrder(int orderId) {
        Order order = null;
        String sql = "SELECT * FROM orders WHERE order_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, orderId);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                order = new Order();
                order.setOrderId(rs.getInt("order_id"));
                order.setUsername(rs.getString("username"));
                order.setOrderDate(rs.getDate("order_date"));
                order.setShipAddress1(rs.getString("ship_address1"));
                order.setShipAddress2(rs.getString("ship_address2"));
                order.setShipCity(rs.getString("ship_city"));
                order.setShipState(rs.getString("ship_state"));
                order.setShipZip(rs.getString("ship_zip"));
                order.setShipCountry(rs.getString("ship_country"));
                order.setBillAddress1(rs.getString("bill_address1"));
                order.setBillAddress2(rs.getString("bill_address2"));
                order.setBillCity(rs.getString("bill_city"));
                order.setBillState(rs.getString("bill_state"));
                order.setBillZip(rs.getString("bill_zip"));
                order.setBillCountry(rs.getString("bill_country"));
                order.setCourier(rs.getString("courier"));
                order.setTotalPrice(rs.getBigDecimal("total_price"));
                order.setCreditCard(rs.getString("credit_card"));
                order.setExpiryDate(rs.getString("expiry_date"));
                order.setCardType(rs.getString("card_type"));
                order.setLocale(rs.getString("locale"));
                order.setStatus(rs.getString("status"));

                // 同样，需要填充订单的 `lineItems`
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public void insertOrder(Order order) {
        String sql = "INSERT INTO orders (username, order_date, ship_address1, ship_address2, ship_city, " +
                "ship_state, ship_zip, ship_country, bill_address1, bill_address2, bill_city, bill_state, " +
                "bill_zip, bill_country, courier, total_price, credit_card, expiry_date, card_type, locale, status) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, order.getUsername());
            stmt.setDate(2, new java.sql.Date(order.getOrderDate().getTime()));
            stmt.setString(3, order.getShipAddress1());
            stmt.setString(4, order.getShipAddress2());
            stmt.setString(5, order.getShipCity());
            stmt.setString(6, order.getShipState());
            stmt.setString(7, order.getShipZip());
            stmt.setString(8, order.getShipCountry());
            stmt.setString(9, order.getBillAddress1());
            stmt.setString(10, order.getBillAddress2());
            stmt.setString(11, order.getBillCity());
            stmt.setString(12, order.getBillState());
            stmt.setString(13, order.getBillZip());
            stmt.setString(14, order.getBillCountry());
            stmt.setString(15, order.getCourier());
            stmt.setBigDecimal(16, order.getTotalPrice());
            stmt.setString(17, order.getCreditCard());
            stmt.setString(18, order.getExpiryDate());
            stmt.setString(19, order.getCardType());
            stmt.setString(20, order.getLocale());
            stmt.setString(21, order.getStatus());

            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insertOrderStatus(Order order) {
        String sql = "UPDATE orders SET status = ? WHERE order_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, order.getStatus());
            stmt.setInt(2, order.getOrderId());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
